package cn.virens.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * 日期工具类 Created by loioi on 2016/12/22.
 */

public class CalendarUtil {
	public static final String Y = "yyyy";
	public static final String YM = "yyyy-MM";
	public static final String YMD = "yyyy-MM-dd";
	public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";
	public static final String HMS = "HH:mm:ss";
	public static final String FORMAT_DEFAULT = "未知";

	private CalendarUtil() {
	}

	/**
	 * 复制一个日期
	 * 
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return
	 */
	public static Calendar clone(int year, int month, int dayOfMonth) {
		return new GregorianCalendar(year, month - 1, dayOfMonth);
	}

	/**
	 * 复制一个日期
	 * 
	 * @param calendar
	 * @return
	 */
	public static Calendar clone(Calendar calendar) {
		Calendar result = Calendar.getInstance(calendar.getTimeZone());
		result.setTimeInMillis(calendar.getTimeInMillis());
		return result;
	}

	/**
	 * 复制一个日期
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar clone(Date date) {
		Calendar result = Calendar.getInstance();
		result.setTime(date);
		return result;
	}

	/**
	 * 判断 - c1 在 c2 之后
	 * 
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static boolean after(Object c1, Object c2) {
		long t1 = 0;
		long t2 = 0;

		if (c1 instanceof Date) {
			t1 = ((Date) c1).getTime();
		} else if (c1 instanceof Calendar) {
			t1 = ((Calendar) c1).getTimeInMillis();
		} else {
			return false;
		}

		if (c2 instanceof Date) {
			t2 = ((Date) c2).getTime();
		} else if (c1 instanceof Calendar) {
			t2 = ((Calendar) c2).getTimeInMillis();
		} else {
			return false;
		}

		return t1 > t2;
	}

	/**
	 * 判断 - c1 在 c2 之前
	 * 
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static boolean before(Object c1, Object c2) {
		long t1 = 0;
		long t2 = 0;

		if (c1 instanceof Date) {
			t1 = ((Date) c1).getTime();
		} else if (c1 instanceof Calendar) {
			t1 = ((Calendar) c1).getTimeInMillis();
		} else {
			return false;
		}

		if (c2 instanceof Date) {
			t2 = ((Date) c2).getTime();
		} else if (c1 instanceof Calendar) {
			t2 = ((Calendar) c2).getTimeInMillis();
		} else {
			return false;
		}

		return t1 < t2;
	}

	/**
	 * 判断是不同一年
	 * 
	 * @param calendar1
	 * @param calendar2
	 * @return
	 */
	public static boolean isSameYear(Calendar calendar1, Calendar calendar2) {
		if (calendar1 == null) return false;
		if (calendar2 == null) return false;

		return (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR));
	}

	/**
	 * 判断是不是上一个月
	 * 
	 * @param calendar1 当前月份
	 * @param calendar2 被判定月份
	 * @return
	 */
	public static boolean isPrevMonth(Calendar calendar1, Calendar calendar2) {
		Calendar calendar = CalendarUtil.clone(calendar2);
		calendar.add(Calendar.MONTH, 1);

		return isSameYear(calendar1, calendar);
	}

	/**
	 * 判断是不同一月
	 * 
	 * @param calendar1
	 * @param calendar2
	 * @return
	 */
	public static boolean isSameMonth(Calendar calendar1, Calendar calendar2) {
		return (isSameYear(calendar1, calendar2) && (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)));
	}

	/**
	 * 判断是不是下一个月
	 * 
	 * @param calendar1 当前月份
	 * @param calendar2 被判定月份
	 * @return
	 */
	public static boolean isNextMonth(Calendar calendar1, Calendar calendar2) {
		Calendar calendar = CalendarUtil.clone(calendar2);
		calendar.add(Calendar.MONTH, -1);

		return isSameMonth(calendar1, calendar);
	}

	/**
	 * 判断是昨天
	 * 
	 * @param calendar1 当前日期
	 * @param calendar2 被判定日期
	 * @return
	 */
	public static boolean isPrevDay(Calendar calendar1, Calendar calendar2) {
		Calendar calendar = CalendarUtil.clone(calendar2);
		calendar.add(Calendar.DAY_OF_MONTH, 1);

		return isSameDay(calendar1, calendar);
	}

	public static boolean isSameDay(Date date1, Date date2) {
		if (date1 == null) return false;
		if (date2 == null) return false;

		Calendar calendar1 = clone(date1);
		Calendar calendar2 = clone(date2);

		return isSameDay(calendar1, calendar2);
	}

	/**
	 * 判断是不同一日
	 * 
	 * @param calendar1
	 * @param calendar2
	 * @return
	 */
	public static boolean isSameDay(Calendar calendar1, Calendar calendar2) {
		return (isSameMonth(calendar1, calendar2) && (calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)));
	}

	/**
	 * 判断是明天
	 * 
	 * @param calendar1 当前日期
	 * @param calendar2 被判定日期
	 * @return
	 */
	public static boolean isNextDay(Calendar calendar1, Calendar calendar2) {
		Calendar calendar = CalendarUtil.clone(calendar2);
		calendar.add(Calendar.DAY_OF_MONTH, -1);

		return isSameDay(calendar1, calendar);
	}

	/**
	 * 将时间格式化
	 * 
	 * @param pattern 格式
	 * @return
	 */
	public static String format(String pattern) {
		return format(new Date(), pattern);
	}

	/**
	 * 将日期格式化
	 * 
	 * @param calendar
	 * @param pattern
	 * @return
	 */
	public static String format(Calendar calendar, String pattern) {
		return format(calendar.getTime(), pattern);
	}

	/**
	 * 将时间格式化
	 * 
	 * @param date 时间
	 * @param pattern 格式
	 * @return
	 */
	public static String format(Date date, String pattern) {
		if (date == null) return null;
		if (TextUtils.isEmpty(pattern)) return null;

		return DateFormatUtils.format(date, pattern);
	}

	/**
	 * 将字符串 转为 Date
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String date, String pattern) {
		if (TextUtils.isEmpty(date)) return null;
		if (TextUtils.isEmpty(pattern)) return null;

		try {
			return DateUtils.parseDate(date, pattern);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * yyyy-MM-dd 格式化为Date
	 * 
	 * @param date
	 * @return
	 */
	public static Date parse(String date) {
		if (TextUtils.isEmpty(date)) {
			return null;
		} else if (YMD_HMS.length() == date.length()) {
			return parse(date, YMD_HMS);
		} else {
			return parse(date, YMD);
		}
	}
}
